<?php

namespace App\Http\Controllers\Admin\Wechat;

use App\Http\Controllers\Admin\BaseController;
use App\Models\WechatMenu;
use App\Http\Requests\Admin\Wechat\Menu\CreateRequest;
use App\Http\Requests\Admin\Wechat\Menu\UpdateRequest;
use Breadcrumbs, Toastr;
class MenuController extends BaseController
{
    public function index(){
        
        /**
         * 获取顶级菜单
         */
        $fmenus = WechatMenu::where('fid','=',0)->orderBy('sort', 'asc')->get();
       
        $menus = [];
        foreach($fmenus as $k=>$fmenu){

            
            $submenus = WechatMenu::where('fid','=',$fmenu->id)->orderBy('sort', 'asc')->get();
            $fmenu['count'] = count($submenus);
            $menus[] = $fmenu;
            foreach($submenus as $m=>$submenu){
                $submenu['count'] = 0 ;
                 $submenu['fname'] = $fmenu->name;
                $menus[] = $submenu;
            }
            
        }
        
        return view('admin.wechat.menu.index',['menus'=>$menus]);
    }

    public function create(){

        /**
         * 获取顶级菜单
         */
        $fmenus = WechatMenu::where('fid','=',0)->get();

        return view('admin.wechat.menu.create',compact('fmenus'));
    }

    public function store(CreateRequest $request){


        //判断一级菜单是否超过3个
        if($request['fid']==0){
            $num = WechatMenu::where('fid', '=', 0)->count();
            if($num>=3){
                $result= [
                    'status' => false,
                    'msg' => '一级菜单不能超过三个'
                ];
                Toastr::error($result['msg']);
                return redirect(route("admin.wechat.menu.create"));
            }
        }else{
            $num = WechatMenu::where('fid', '=', $request['fid'])->count();
            if($num>=5){
                $result= [
                    'status' => false,
                    'msg' => '二级菜单不能超过五个'
                ];
                Toastr::error($result['msg']);
                return redirect(route("admin.wechat.menu.create"));
            }
        }

         $result = WechatMenu::create([
            'fid'=>$request['fid'],
            'name'=>$request['name'],
            'url'=>$request['url'],
            'key'=>$request['key'],
            'sort'=>$request['sort'],
            'type'=>$request['type'],
            'media_id'=>$request['media_id']
        ]);
        if(!$result){
            Toastr::error('添加失败!');
            return redirect(route("admin.wechat.menu.create"));
        }
        Toastr::success('添加成功!');
        return redirect('admin/wechat/menu');
    }


    public function edit($id){
        $menu = WechatMenu::find($id);
        //dd(11);
        $fmenus = WechatMenu::where('fid','=',0)->get();
        return view("admin.wechat.menu.edit",['menu'=>$menu,'fmenus'=>$fmenus]);
    }

    public function update(UpdateRequest $request,$id){
         $data = [
            'fid'=>$request['fid'],
            'name'=>$request['name'],
            'url'=>$request['url'],
            'key'=>$request['key'],
            'sort'=>$request['sort'],
            'type'=>$request['type'],
            'media_id'=>$request['media_id']
        ];

        //判断一级菜单是否超过3个
        if($request['fid']==0){
            $num = WechatMenu::where('id', '<>', $id)->where('fid', '=', 0)->count();
            if($num>=3){
                $result= [
                    'status' => false,
                    'msg' => '一级菜单不能超过三个'
                ];
                Toastr::error($result['msg']);
                return redirect(route("admin.wechat.menu.edit",['id' => $id]));
            }
        }else{
            if($request['fid']==$id){
                $result= [
                    'status' => false,
                    'msg' => '不能成为自己的上级'
                ];
                Toastr::error($result['msg']);
                return redirect(route("admin.wechat.menu.edit",['id' => $id]));
            }
            $num = WechatMenu::where('id', '<>', $id)->where('fid', '=', $request['fid'])->count();
            if($num>=5){
                $result= [
                    'status' => false,
                    'msg' => '二级菜单不能超过五个'
                ];
                Toastr::error($result['msg']);
                return redirect(route("admin.wechat.menu.edit",['id' => $id]));
            }
        }

        $isAble = WechatMenu::where('id', '<>', $id)->where('name', $data['name'])->count();
        if($isAble) {
           $result= [
                'status' => false,
                'msg' => '名称已存在'
            ];
            Toastr::error($result['msg']);
            return redirect(route("admin.wechat.menu.edit",['id' => $id]));
        }

        $result = WechatMenu::where('id','=',$id)->update($data);

        if(!$result){
            Toastr::error('编辑失败!');
            return redirect(route("admin.wechat.menu.edit",['id' => $id]));
        }
        Toastr::success('编辑成功!');
        return redirect('admin/wechat/menu');
    }

    public function destroy($id)
    {
        //var_dump($id);
        $menu = WechatMenu::find($id);
        //var_dump($menu);
        if(!$menu){
            return response()->json(['status' => 0]);
        }
        $result = $menu->delete();
        //var_dump($result);
        //$result = $this->reply->delete($id);
        return response()->json($result ? ['status' => 1] : ['status' => 0]);
    }

    /**
     * [sysncmenu 同步菜单 ]
     * @return [type] [description]
     */
    public function sysnc(){
        $fmenus = WechatMenu::where('fid','=',0)->orderBy('sort', 'asc')->get();
        $menus = [];
        foreach($fmenus as $k=>$fmenu){
            $pmenu = ['type'=>$fmenu->type,"name"=>$fmenu->name,"url"=>$fmenu->url,"key"=>$fmenu->key];
            $submenus = WechatMenu::where('fid','=',$fmenu->id)->orderBy('sort', 'asc')->get();
            if($submenus){
                $subs = [];
                foreach($submenus as $m=>$submenu){
                    $sub = ['type'=>$submenu->type,"name"=>$submenu->name,"url"=>$submenu->url,"key"=>$submenu->key];
                    $subs[] = $sub;
                }
                $pmenu["sub_button"] = $subs;
            }
            $menus[] = $pmenu;
        }
        //dd($menus);
        $wechat = app('wechat');
        $menu = $wechat->menu;
        $menu->add($menus);
        Toastr::success('已点击同步，请重新关注公众号查看!');
        return redirect('admin/wechat/menu');
        //$menus = $menu->current();
        //$menus = $menu->all();
    }

}